package com.resou.reader.mine.login;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.resou.reader.R;
import com.resou.reader.api.entry.SortDataBean;
import com.resou.reader.api.entry.SortSubBean;
import com.resou.reader.base.v.BaseActivity;
import com.resou.reader.mine.IView.IFavoriteView;
import com.resou.reader.mine.presenter.FavoritePresenter;
import com.resou.reader.view.Loading;
import com.resou.reader.view.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FavoriteActivity extends BaseActivity implements IFavoriteView {

    @BindView(R.id.favorite_tab)
    PagerSlidingTabStrip mTabLayout;
    @BindView(R.id.favorite_pager)
    ViewPager mViewPager;
    private List<FavoriteFragment> fragments = new ArrayList<>();

    @Override
    protected void initView() {
        Loading.getInstance().show(getSupportFragmentManager());
    }

    @Override
    protected void retryRefreshData() {

    }

    @Override
    protected void initPresenter() {
        presenter = new FavoritePresenter(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_favorite;
    }

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, FavoriteActivity.class));
    }

    @OnClick({R.id.title_right_btn})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_right_btn:
                updateToServer(null);
                break;
            default:
                break;
        }
    }

    private void updateToServer(String[] args) {
//        Toast.makeText(this, getString(R.string.wait_server), Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public Context getViewContext() {
        return this;
    }

    @Override
    public void refresh(final List<SortDataBean<List<SortSubBean>>> data) {
        Loading.getInstance().dismiss();
        if (data == null || data.size() <= 0)
            return;
        for (int i = 0; i < data.size(); i++) {
            FavoriteFragment fragment = new FavoriteFragment();
            fragment.setData(data.get(i).getSubSort());
            fragments.add(fragment);
        }
        FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return data.get(position).getFreetypeName();
            }
        };
        fragmentPagerAdapter.getPageTitle(0);
        mViewPager.setAdapter(fragmentPagerAdapter);
        mTabLayout.setViewPager(mViewPager);
    }
}
